/**
 * 
 */
package com.carnation.qa.socketserver.proxy.moi;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.carnation.mong.platform.domain.MOIDataRevision;
import com.carnation.mong.platform.service.DataRevisionService;
import com.carnation.qa.socketserver.domain.Item;
import com.carnation.qa.socketserver.proxy.Builder;

/**
 * @author Nick
 * 
 */
public class DataRevisionServiceImpl extends AbstractMOIServiceImpl {

	private final Log log = LogFactory.getLog(getClass());

	private DataRevisionService service;

	public DataRevisionServiceImpl(List<Item> items) {
		super(items);
		service = ServiceFactory.getService(DataRevisionService.class);
	}

	@Override
	protected String invoke() {
		Item item = getItems().get(0);
		String method = getMethodName(item);
		String returnStr;
		if ("getRevision".equals(method)) {
			returnStr = getRevision(item);
		} else if ("getRevisions".equals(method)) {
			returnStr = getRevisions(item);
		} else if ("update".equals(method)) {
			returnStr = update(item);
		} else {
			throw new IllegalArgumentException(method);
		}
		return returnStr;
	}

	private String getRevision(Item item) {
		if (item.getChildren().size() == 1) {// 1个参数
			String revisionId = getString(item.getChildren().get(0)
					.getLabelParts()[1]);
			if (log.isDebugEnabled()) {
				log.debug("revisionId:" + revisionId);
			}
			Object obj = null;
			try {
				obj = service.getRevision(revisionId);
				if (log.isDebugEnabled()) {
					log.debug(obj);
				}
				return new Builder(obj, null, item.getTabSeparator())
						.toString();
			} catch (Exception e) {
				if (log.isErrorEnabled()) {
					log.error(item.toString(), e);
				}
				return e.toString();
			}
		}
		return IllegalArgumentException.class.getSimpleName();
	}

	private String getRevisions(Item item) {
		if (item.getChildren().size() == 1) {// 1个参数
			String dataId = getString(item.getChildren().get(0).getLabelParts()[1]);
			if (log.isDebugEnabled()) {
				log.debug("dataId:" + dataId);
			}
			Object obj = null;
			try {
				obj = service.getRevisions(dataId);
				if (log.isDebugEnabled()) {
					log.debug(obj);
				}
				return new Builder(obj, null, item.getTabSeparator())
						.toString();
			} catch (Exception e) {
				if (log.isErrorEnabled()) {
					log.error(item.toString(), e);
				}
				return e.toString();
			}
		}
		return IllegalArgumentException.class.getSimpleName();
	}

	private String update(Item item) {
		if (item.getChildren().size() == 1) {// 1个参数
			MOIDataRevision r = instanceMOIDataRevision(item.getChildren().get(
					0));
			if (log.isDebugEnabled()) {
				log.debug("moidataRevision:" + r);
			}
			Object obj = null;
			try {
				service.update(r);
				obj = service.getRevision(r.getId());
				if (log.isDebugEnabled()) {
					log.debug(obj);
				}
				return new Builder(obj, null, item.getTabSeparator())
						.toString();
			} catch (Exception e) {
				if (log.isErrorEnabled()) {
					log.error(item.toString(), e);
				}
				return e.toString();
			}
		}
		return IllegalArgumentException.class.getSimpleName();
	}

}
